(function(){
	var pagenum=10;//每页数量
	var curpage=1;//当前页面
	var totalPage=0;//总页数
	//获取列表
	//http://web.icecn.net/lottery/add.json

	var params = {
		gameId : "" ,
		teamName : "" ,
		beginDate : "" ,
		endDate : "" ,
		offset : 0 ,
		limit : pagenum
	}
	var doneId = "";
	var closeId = "";
	var lottList = [];
	function getlist(curpage,pagenum){
		$(".box-body").mLoading();
	   $.ajax({
			url: httpOrigin + '/lottery/list.json',
			type:'POST',
			async:true,
			data: params ,
			dataType: "json",
			success: function(data){
				var html = "";
				if(data.code === 200 ){
					lottList = data.t.result;

					if(lottList && lottList.length > 0){
						lottList.forEach(function ( item ,index ) {
							var tr = '',first = false,inner = "",inner2 = "",tr2 = "";
							//var first = item.num.shift();

							item.handicapInfos.forEach(function ( val ,key ) {
								var time = new Date(val.quizEndDate).format("yyyy-MM-dd hh:mm:ss");
								var goldType = "",status = "",isShow = "";
								if( val.goldType == 0 ) {
									goldType = "金币";
								} else if ( val.goldType == 1 ) {
									goldType = "钻石";
								}
								if( val.status == 0 ) {
									status = "竞猜中";
								} else if( val.status == 3 ) {
									status = "已结束";
								} else {
									status = "赛事中";
								}
								if( item.isShow == 1 ) {
									isShow = "是";
								}
								if( item.isShow == 0 ) {
									isShow = "否";
								}
								if( !first ) {
									inner += "<td>" + val.handicapType + "</td>" +
											"<td>" + val.id + "</td>" +
											"<td>" + goldType + "</td>" +
											"<td>" + val.redLotteryObbs + " : " + val.blueLotteryObbs + "</td>" +
											"<td>" + status + "</td>" +
											"<td>" + time + "</td>" +
											"<td>" + val.redCount + "</td>" +
											"<td>" + val.blueCount + "</td>";
									first = true;
								} else {
									tr += "<tr role=\"row\">" +
												"<td>" + val.handicapType + "</td>" +
												"<td>" + val.id + "</td>" +
												"<td>" + goldType + "</td>" +
												"<td>" + val.redLotteryObbs + " : " + val.blueLotteryObbs + "</td>" +
												"<td>" + status + "</td>" +
												"<td>" + time + "</td>" +
												"<td>" + val.redCount + "</td>" +
												"<td>" + val.blueCount + "</td>" +
												"<td class=\"toggle-show\" data-id=\""+ val.id +"\" data-show=\""+ val.filedShow +"\">"+ isShow +"</td>" +
												"<td>" +
												"<a href=\"../../html/QuizManagement/QuizNew.html?id="+ item.id +"\" target='_blank'><button type=\"button\" class=\"btn btn-info btn-xs\" data-toggle=\"modal\" data-target=\"#modal-addupdate\">修改比赛信息</button></a>" +
												"<button type=\"button\" class=\"btn btn-info btn-xs close-lott\" data-id=\""+ val.id +"\" data-target=\"#modal-delete\" data-toggle=\"modal\">流盘</button>" +
												"<button type=\"button\" class=\"btn btn-info btn-xs done-lott\" data-id=\""+ val.id +"\" data-target=\"#modal-addupdate\" data-toggle=\"modal\">结算</button>" +
												"</td>"
											"</tr>";
								}
							}, this );
							html += "<tr role=\"row\">" +
										"<td rowspan=\""+ item.handicapInfos.length +"\">自营</td>" +
										"<td rowspan=\""+ item.handicapInfos.length +"\">"+ item.name +"</td>" +
										"<td rowspan=\""+ item.handicapInfos.length +"\">"+ item.red +" vs " + item.blue + "</td>" +
										"<td rowspan=\""+ item.handicapInfos.length +"\">"+ item.quizId + "</td>" +
										inner +
										"<td class=\"toggle-show\" data-id=\""+ item.handicapInfos[0].id +"\" data-show=\""+ item.handicapInfos[0].filedShow +"\">"+ ( item.handicapInfos[0].filedShow == 1 ? '是' : '否' ) + "</td>" +
										"<td rowspan=\""+ item.handicapInfos.length +"\" class=\"sort-top\" data-id=\""+ item.id +"\">"+ item.sortId +"</td>" +
										"<td>" +

											"<a href=\"../../html/QuizManagement/QuizNew.html?id="+ item.id +"\" target='_blank'><button type=\"button\" class=\"btn btn-info btn-xs\">修改比赛信息</button></a>" +
											"<button type=\"button\" class=\"btn btn-info btn-xs close-lott\" data-id=\""+ item.handicapInfos[0].id +"\" data-target=\"#modal-delete\" data-toggle=\"modal\">流盘</button>" +
											"<button type=\"button\" class=\"btn btn-info btn-xs done-lott\" data-id=\""+ item.handicapInfos[0].id +"\" data-target=\"#modal-addupdate\" data-toggle=\"modal\">结算</button>" +
											"</td>" +
									"</tr>" +
									tr;
						} , this )

					   totalPage=data.t.totalPage;
					   $(".pageinfo").html("当前"+curpage+"页/共"+totalPage+"页");
					   $(".curpage").val(curpage);
					   if(curpage==1){$(".prev").addClass("disabled")}else{$(".prev").removeClass("disabled")}
					   if(curpage >= totalPage){$(".next").addClass("disabled")}else{$(".next").removeClass("disabled")}
					} else {
						html += "<tr><td colspan='14'>暂无数据</td></tr>";

						totalPage=data.t.totalPage;
						$(".pageinfo").html("当前"+curpage+"页/共"+totalPage+"页");
						$(".curpage").val(curpage);
						if(curpage==1){$(".prev").addClass("disabled")}else{$(".prev").removeClass("disabled")}
						if(curpage >= totalPage){$(".next").addClass("disabled")}else{$(".next").removeClass("disabled")}
					}
					$("#example1 tbody").html( html );
				} else {
					alert( data.msg );
				}

			} ,
		   complete : function () {
			   $(".box-body").mLoading("hide");
		   }
	   });
	}
	//流盘接口
	 function closeLott( _params ) {
		 $(".box-body").mLoading();
	 	$.ajax({
	 		url : httpOrigin + "/lottery/repeal.json" ,
	 		type : "POST" ,
	 		data : _params ,
	 		dataType : "json" ,
	 		success : function ( data ) {
	 			if( data.code == 200 ) {
	 				alert( data.msg );
	 				getlist( curpage , totalPage );
	 			} else {
					alert( data.msg );
				}
	 		} ,
			complete : function () {
				$(".box-body").mLoading("hide");
			}
	 	})
	 }
	//结算接口
	function doneLott( _params ) {
		$(".box-body").mLoading();
		$.ajax({
			url : httpOrigin + "/lottery/clearingMatch.json" ,
			type : "POST" ,
			data : _params ,
			dataType : "json" ,
			success : function ( data ) {
				if( data.code == 200 ) {
					$(".remarks").val("");
					$(".red-team")[0].checked;
					//$(".modal").hide();
					getlist( curpage , totalPage );
				} else {

				}
				alert( data.msg );
			} ,
			complete : function () {
				$(".box-body").mLoading("hide");
			}
		})
	}
	//批量显示
	function mostShow( _params ) {
		$(".box-body").mLoading();
		$.ajax({
			url : httpOrigin + "/lottery/isShow.json" ,
			type : "POST" ,
			data : _params ,
			dataType : "json" ,
			success : function ( data ) {
				if( data.code == 200 ) {
					getlist(curpage, totalPage);
				} else {
					$(".box-body").mLoading("hide");
					alert( data.msg );
				}

			}
		})
	}
	//排序接口
	function setLottSort( _params ){
		$(".box-body").mLoading();
		$.ajax({
			url : httpOrigin + "/lottery/update/sortId.json" ,
			type :"POST" ,
			data : _params ,
			success : function ( data ) {
				if( data.code == 200 ) {
					getlist(curpage, totalPage);
				} else {
					$(".box-body").mLoading("hide");
					alert( data.msg );
				}
			}
		})
	}
	//游戏类型
	function getGames() {
		$.ajax({
			url: httpOrigin + '/gameinfo/list.json',
			type:'POST',
			async:true,
			data : {
				applySite : 1
			} ,
			success : function ( data ) {
				var games = $(".games");
				var html = "<option value=\"\">全部</option>";
				if( data.code == 200 ) {
					if( data.hasOwnProperty("t") && data.t.hasOwnProperty("gameList") ) {
						for( var i = 0; i < data.t.gameList.length; i ++ ) {
							html += "<option value=\""+ data.t.gameList[i].id +"\">"+ data.t.gameList[i].name +"</option>";
						}
						games.html( html );
					} else {
						alert( "无法获取游戏列表" );
					}
				} else {
					alert( data.msg );

				}
			} ,
			error : function ( err ) {
				alert( err.msg );
			}
		})
	}
	var Page = {
		init:function(){
			this.render();
			this.event();
		},
		//绑定事件
		event:function(){
			//上一页
		    $(".prev").click(
			    function(){
					if(curpage>1){
				     	curpage=curpage-1;
						params.offset = ( curpage - 1 ) * params.limit;
					    getlist(curpage,pagenum);
					}
			    }
			)
			//下一页
		    $(".next").click(
			    function(){
					if(curpage<totalPage){
				     	curpage=curpage+1;
						params.offset = ( curpage - 1 ) * params.limit;
					    getlist(curpage,pagenum);
					}
			    }
			)
			//页码跳转
			$(".gopage").click(
			    function(){
					if($(".curpage").val()>=1&&$(".curpage").val()<=totalPage){
				    	curpage=parseInt($(".curpage").val());
						params.offset = ( curpage - 1 ) * params.limit;
					    getlist(curpage,pagenum)
					}else{
						$(".curpage").val(curpage);
					}
			    }
			)
			//批量显示
			//$(".most-show").click(function () {
			//
			//})
			//流盘
			$(".ok-btn1").click(function () {
				closeLott( { handicapId : closeId } );
			})
			$("#example1").on( "click" ,".close-lott",function () {
				closeId =  $( this).get(0).dataset.id;
			})
			$("#example1").on( "click" ,".done-lott",function () {
				var id =  $( this).get(0).dataset.id;
				lottId = id;
			})
			//显示隐藏
			$("#example1").on( "click" ,".toggle-show",function () {
				var id =  $( this).get(0).dataset.id,
					show = $(this).get(0).dataset.show,
					toggle = 0;
				var tip = "";
				if( show == 1 ) {
					tip = "确定隐藏？";
					toggle = 0;
				} else {
					tip = "确定显示？";
					toggle = 1;
				}
				var result = window.confirm( tip );

				if( result ) {
					mostShow({ handicapIds : id , isShow : toggle });
				}
			})
			//排序
			$("#example1").on( "click" ,".sort-top",function () {
				var id =  $( this).get(0).dataset.id;
				var result = window.prompt( "请输入权重" );

				if( result ) {
					setLottSort({ lotteryId : id , sortId : result });
				}
			})
			//结算
			$(".sub-btn").on( "click" ,function () {
				var params = {
					handicapId : lottId ,
					redResult : $(".red-team")[0].checked ? 0 : 1 ,
					resultNot : $(".remarks").val()
				}
				doneLott( params );
			})

			//筛选游戏类型
			$(".games").change(function () {
				params.gameId = Boolean( $( this).val() ) ? $( this).val() : "";
			})
			//比赛队伍
			$(".player").change(function () {
				params.teamName = $( this).val();
			})
			//开始日期
			$("#datepicker").change(function () {
				params.beginDate = $( this).val();
			})
			//结束日期
			$("#datepicker2").change(function () {
				params.endDate = $( this).val();
			})
			//搜索按钮
			$(".search-btn").click(function () {
				curpage = 1;
				params.offset = 0;
				getlist(curpage,pagenum)
			})
		},
		//渲染页面
		render:function(){
			getlist(curpage,pagenum)
			getGames();
		}
	};
	Page.init();
})();
